package org.viettan.android.main.activities.radio;

import org.viettan.android.main.R;
import org.viettan.android.main.activities.common.VietTanBaseActivity;
import org.viettan.android.main.io.Logger;
import org.viettan.android.main.managers.RadioManager;
import org.viettan.android.main.model.RadioEpisode;
import org.viettan.android.main.services.RadioProgressListener;
import org.viettan.android.main.view.ViewUtility;

import roboguice.inject.InjectView;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.google.inject.Inject;

public class RadioEpisodeActivity extends VietTanBaseActivity implements OnSeekBarChangeListener {

    @InjectView(R.id.playPause)
    Button playPauseButton;

    @InjectView(R.id.stop)
    Button stopButton;

    @InjectView(R.id.image)
    ImageView radioCtmBanner;

    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.subtitle)
    TextView subtitle;

    @InjectView(R.id.time)
    TextView time;

    @InjectView(R.id.progressBar)
    SeekBar progressBar;

    @Inject
    private RadioManager radioManager;

    @Inject
    private Logger logger;

    @Inject
    private ViewUtility viewUtility;

    private boolean playbackPaused;
    private boolean pauseProgressUpdates;
    private RadioProgressListener.Stub progressListener;

    @Override
    public void onResume() {
	super.onResume();

	setContentView(R.layout.radioepisode);
	viewUtility.scaleImageToDeviceWidth(radioCtmBanner);

	RadioEpisode currentEpisode = radioManager.getCurrentlyPlaying();
	if (currentEpisode == null) {
	    // Ideally this should not happen
	    logger.warn("Navigated to RadioEpisodeActivity without an active RadioEpisode");
	} else {
	    title.setText(currentEpisode.getTitle());
	    subtitle.setText(String.format("[%s] %s", new Object[] { currentEpisode.getSubtitle(),
		    currentEpisode.getAuthor() }));
	}

	playPauseButton.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		if (playbackPaused) {
		    radioManager.resume();
		    playPauseButton.setBackgroundResource(R.drawable.radio_pause);
		} else {
		    radioManager.pause();
		    playPauseButton.setBackgroundResource(R.drawable.radio_play);
		}

		playbackPaused = !playbackPaused;
	    }
	});

	stopButton.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		handleStop();
	    }
	});

	// set up listener so that we update the progress bar as we play
	progressListener = new RadioProgressListener.Stub() {
	    public void onProgressUpdate(final int current, final int max) throws RemoteException {
		// don't update the progress while the user is seeking in the bar
		if (pauseProgressUpdates) {
		    return;
		}
		runOnUiThread(new Runnable() {
		    public void run() {
			progressBar.setProgress(current);
			progressBar.setMax(max);
			time.setText(String.format("%s (%s)", ViewUtility.getTime(current),
				ViewUtility.getTime(max)));
		    }
		});
	    }

	    public void onPlaybackCompleted() throws RemoteException {
		handleStop();
	    }
	};
	radioManager.addRadioProgressListener(progressListener);

	// set up code to send seeking commands to the service
	progressBar.setOnSeekBarChangeListener(this);
    }

    /**
     * Stops the playback of the current radio episode and returns to the previous activity.
     */
    public void handleStop() {
	radioManager.removeRadioProgressListener(progressListener);
	radioManager.stop();
	RadioEpisodeActivity.this.finish();
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
	if (fromUser) {
	    time.setText(ViewUtility.getTime(progress));
	}
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
	pauseProgressUpdates = true;
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
	radioManager.seekTo(seekBar.getProgress());
	pauseProgressUpdates = false;
    }
}
